package Arrays;

import java.util.Stack;

/**
 * 作者:hfj
 * 功能:20.有效的括号
 * 日期: 2025/10/17 11:16
 */
public class IsValid {
    public static void main(String[] args) {
        String s = "([)]";
        System.out.println(isValid(s));
    }

    public static boolean isValid(String s) {
        //利用栈来判断是否括号匹配
        Stack<Character> stack = new Stack<>();
        //入栈
        for (char c:s.toCharArray()){
            if (c == '('){
                stack.push(')');
            } else if (c == '[') {
                stack.push(']');
            } else if (c == '{') {
                stack.push('}');
            } else if (stack.isEmpty() || stack.pop() != c) { //判断非空和是否一致
                return false;
            }
        }
        return stack.isEmpty();
    }
}
